package org.showminifier.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.showminifier.web.PMF;
import org.showminifier.web.PagePerformance;
import org.showminifier.web.Util;
import org.showminifier.web.WebContent;
import org.showminifier.web.WebContentUtil;

/**
*
* Show Minifier
* Author: Shihua Ma <beijing.josh@gmail.com>
* Copyright (c) 2010 Shihua Ma
* Licensed under the MIT license.
*
*/

public class BeaconServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException, ServletException{
		Long keyId = null;
		try{
			keyId = Long.parseLong(req.getParameter("key"));
		}catch( NumberFormatException e){
			resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		String browser = req.getParameter("browser");
		String version = req.getParameter("version");
		String userAgent = req.getHeader("user-agent");
		int originalPerformance = -1;
		try{
			originalPerformance = Integer.parseInt(req.getParameter("original"));
		}catch(NumberFormatException e){};
		
		int minifiedPerformance = -1;
		try{
			minifiedPerformance = Integer.parseInt(req.getParameter("minified"));
		}catch(NumberFormatException e){};
		
		int originalReflowTime = -1;
		try{
			originalReflowTime = Integer.parseInt(req.getParameter("originalReflow"));
		}catch(NumberFormatException e){};
		
		int  minifiedReflowTime = -1;
		try{
			minifiedReflowTime = Integer.parseInt(req.getParameter("minifiedReflow"));
		}catch(NumberFormatException e){};
		
		int originalLatency = -1;
		try{
			originalLatency = Integer.parseInt(req.getParameter("originalLatency"));
		}catch(NumberFormatException e){};
		
		int minifiedLatency = -1;
		try{
			minifiedLatency = Integer.parseInt(req.getParameter("minifiedLatency"));
		}catch(NumberFormatException e){};
		
		Date requestDate = new Date();
		String ipAddress = Util.getClientAddress(req);
		
		PagePerformance perf = new PagePerformance();
		perf.setUserAgent(userAgent);
		perf.setBrowser(browser);
		perf.setVersion(version);
		if(originalPerformance > 0)
			perf.setOriginalPerformance(originalPerformance);
		if(minifiedPerformance > 0)
			perf.setMinifiedPerformance(minifiedPerformance);
		if(originalReflowTime > 0)
			perf.setOriginalReflowTime(originalReflowTime);
		if(minifiedReflowTime > 0)
			perf.setMinifiedReflowTime(minifiedReflowTime);
		if(originalLatency > 0){
			perf.setOriginalLatency(originalLatency);
		}
		if(minifiedLatency > 0){
			perf.setMinifiedLatency(minifiedLatency);
		}
		perf.setRequestDate(requestDate);
		perf.setIpAddress(ipAddress);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		WebContent content = null;
		try{			
			content = pm.getObjectById(WebContent.class,keyId);
			if(content != null){
				perf.setWebContent(content);
				List<PagePerformance> perfs = content.getPerformance();
				perfs.add(perf);
				content.setPerformance(perfs);
			}
		}catch(JDOObjectNotFoundException e){
		}finally {
            pm.close();
        }
		resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}

}
